/*
	合并两个“其数据元素按值非递减有序排列”的有序表(顺序表版)
*/

#include<stdio.h>
#include<malloc.h>
#include"..\Sequence_List\zyf_SqList.h"

void MergerList_SqList(SqList *La, SqList *Lb, SqList *Lc)
{
	int i,j,k;
	i = j = 0;
	k = 1;
	//printf("i的值为%d\n\n",i);
	while (i < La -> length && j < Lb -> length)
	{
		if (La -> elem_array[i] < Lb -> elem_array[j])
		{
			printf("i的值为%d\n",i);
			printf("La的第%d个值为:%d\n",i,La -> elem_array[i]);
			Insert_SqList(Lc,k++,La -> elem_array[i++]);
		}
		else 
		{
			Insert_SqList(Lc,k++,Lb -> elem_array[j++]);
		}
	}

	while (i == La -> length)
	{	
		if (j < Lb -> length)
		{
			Insert_SqList(Lc,k++,Lb -> elem_array[j++]);
		}
		else
			break;
	}

	while (j == Lb -> length)
	{	
		if (i < La -> length)
		{
			Insert_SqList(Lc,k++,La -> elem_array[i++]);
		}
		else
			break;
	}

}

int main(void)
{
	/*
	int b,c,d;
	struct sqlist a;

	b = Init_SqList(&a);

	c =Insert_SqList(&a,1,2);

	d = Delete_SqList(&a,1);

	printf("%d\n",a.length);

	printf("%d    %d  %d  %d\n",b,c,d,a.elem_array[0]);
	*/

	struct sqlist La;
	struct sqlist Lb;
	struct sqlist Lc;

	printf("初始化状态为%d\n",Init_SqList(&La));
	printf("初始化状态为%d\n",Init_SqList(&Lb));
	printf("初始化状态为%d\n\n",Init_SqList(&Lc));

	printf("添加元素状态%d\n",Insert_SqList(&La,1,2));
	printf("添加元素状态%d\n",Insert_SqList(&La,2,3));
	printf("添加元素状态%d\n",Insert_SqList(&La,3,6));
	printf("添加元素状态%d\n",Insert_SqList(&La,4,8));

	printf("添加元素状态%d\n",Insert_SqList(&Lb,1,4 ));
	printf("添加元素状态%d\n",Insert_SqList(&Lb,2,5 ));
	printf("添加元素状态%d\n",Insert_SqList(&Lb,3,8 ));
	printf("添加元素状态%d\n",Insert_SqList(&Lb,4,9 ));
	printf("添加元素状态%d\n",Insert_SqList(&Lb,5,10));
	printf("添加元素状态%d\n",Insert_SqList(&Lb,6,11));

	MergerList_SqList(&La,&Lb,&Lc);

	int i;
	for (i = 0;i < Lc.length;i++)
	{
		printf("第%d值是:%d\n",i+1,Lc.elem_array[i]);
	}
}